package vip.fuxk.blog.common;

import cn.zhxu.bs.BeanSearcher;
import cn.zhxu.bs.SearchResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.solon.plugins.pagination.Page;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.core.handle.Context;

import java.util.Map;

import static vip.fuxk.blog.config.BsPageSizeExtractor.BEAN_SEARCHR_SIZE;

@Component
public class FuckBeanSearcher {

    @Inject
    BeanSearcher beanSearcher;

    private <T> IPage<T> resultPage(SearchResult<T> searchResult){
        FuckPage<T> page = new FuckPage<>(searchResult.getSummaries());
        page.setTotal(searchResult.getTotalCount().intValue());
        page.setRecords(searchResult.getDataList());
        Integer size = Context.current().attr(BEAN_SEARCHR_SIZE);
        page.setSize(size.longValue());
        return page;
    }

    public <T> IPage<T> search(Class<T> beanClass){
        return resultPage(beanSearcher.search(beanClass));
    }
    public <T> IPage<T> search(Class<T> beanClass, Map<String, Object> paraMap){
        return resultPage(beanSearcher.search(beanClass,paraMap));
    }

}
